package com.fowo.api.user.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.sys.model.UserSetting;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Tag(name = "用户配置")
@RestController
@RequestMapping("/api/user-setting")
public class UserSettingController {
    @Resource
    private SysUserService userService;

    @GetMapping("{key}")
    @Operation(summary = "获取当前用户配置")
    public R<UserSetting> getSetting(@PathVariable String key, JwtUserInfo jwtUserInfo){
        if (jwtUserInfo == null) {
            return R.fail("用户未登录");
        }
        return R.success("操作成功", userService.getSetting(jwtUserInfo.getUserId(), key));
    }

    @PutMapping("{key}")
    @Operation(summary = "更新当前用户配置")
    public R updateSetting(@PathVariable String key, @RequestBody UserSetting setting, JwtUserInfo jwtUserInfo) {
        if (jwtUserInfo == null) {
            return R.fail("用户未登录");
        }
        return R.run(() -> userService.updateSetting(jwtUserInfo.getUserId(), key, setting));
    }
}
